
import { _decorator, Component, Node, Mesh, gfx, v3 } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
const { ccclass, property } = _decorator;

const r: number = 0.5;
const ParticleData = {
    positions: [
        -r, -r, 0,
        r, -r, 0,
        r, r, 0,
        -r, r, 0
    ],
    uvs: [
        0, 1,
        1, 1,
        1, 0,
        0, 0
    ],
    indices: [
        0, 1, 2,
        0, 2, 3
    ],
};

/**汽车尾气 */
@ccclass('WeiQi')
export class WeiQi extends yyComponent {

    //#region 网格数据
    protected meshData;
    protected mesh: Mesh;
    protected createMesh() {
        this.meshData = {
            positions: [],
            indices: [],
            uvs: [],
            customAttributes: [],
        };
        this.meshData.customAttributes.push(this.createCustomAttribute(attributeType.move1));
        this.meshData.customAttributes.push(this.createCustomAttribute(attributeType.move2));
        this.meshData.customAttributes.push(this.createCustomAttribute(attributeType.scale));
        this.meshData.customAttributes.push(this.createCustomAttribute(attributeType.time));


    }
    protected createCustomAttribute(type) {
        return {
            attr: {
                format: gfx.Format.RGBA32F,
                isNormalized: false,
                sInstanced: false,
                name: attributeName[type],
            },
            values: [],
        };
    }
    protected pushParticle(t: number) {
        let vertCount = this.meshData.positions.length / 3;
        this.meshData.positions = this.meshData.positions.concat(ParticleData.positions);
        this.meshData.uvs = this.meshData.uvs.concat(ParticleData.uvs);
        for (let i = 0; i < 6; ++i) {
            this.meshData.indices.push(ParticleData.indices[i] + vertCount);
        }
        //自定义动画参数
        //move1
        let s = 1;//Z轴移动速度
        let moveScope = v3(1, 1, 3);
        let p = v3();
        p.x = (Math.random() - 0.5) * moveScope.x;
        p.y = (Math.random() - 0.5) * moveScope.y;
        p.z = Math.random() * moveScope.z;
        //移动时间
        let d = Math.random() * 0.1 + p.z / s;
        // this.meshData.customAttributes[attributeType.move1].values.push()


    }
    //#endregion

}
//自定义属性类型，对应该属性在customAttributes中的索引
enum attributeType {
    move1,
    move2,
    scale,
    time,
};

const attributeName = [
    "a_target1",
    "a_target2",
    "a_scale",
    "a_time",
];

